package com.qian.controller;

import com.qian.pojo.Employees;
import com.qian.service.UserService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    //根据姓名查询用户,模糊查询
    @RequestMapping(value = "/queryUserByName", method = RequestMethod.POST)
    public ResponseEntity queryUserByName(@RequestBody Map map) {
        String name = (String) map.get("employeeName");
        List<Employees> list = userService.searchUserByName(name);
        List<String> empId = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            empId.add(list.get(i).getEmployeeId().toString());
        }
        HashMap res = new HashMap();
        res.put("data", list);
        res.put("empId", empId);
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //增加用户
    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ResponseEntity addUser(@RequestBody Map map, HttpSession session) {
        Employees employees = new Employees();
        employees.setEmployeeAccount((String) map.get("employeeAccount"));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeCompany((String) session.getAttribute("employeeCompany"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        employees.setAuthority(0);
        userService.addUser(employees);
        HashMap res = new HashMap();
        res.put("msg", "添加成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //删除用户
    @RequestMapping(value = "/deleteUser", method = RequestMethod.POST)
    public ResponseEntity deleteUser(@RequestBody Map map) {
        Long employeeId = Long.parseLong((String) map.get("employeeId"));
        userService.deleteUser(employeeId);
        HashMap res = new HashMap();
        res.put("msg", "删除成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //更新用户
    @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
    public ResponseEntity updateUser(@RequestBody Map map) {
        Employees employees = userService.searchUserById(Long.parseLong((String) map.get("employeeId")));
        employees.setEmployeePassword((String) map.get("employeePassword"));
        employees.setEmployeeName((String) map.get("employeeName"));
        employees.setEmployeeGender((String) map.get("employeeGender"));
        employees.setEmployeeEmail((String) map.get("employeeEmail"));
        employees.setEmployeeAddress((String) map.get("employeeAddress"));
        employees.setEmployeePosition((String) map.get("employeePosition"));
        userService.updateUser(employees);
        HashMap res = new HashMap();
        res.put("msg", "更新成功");
        return new ResponseEntity(res, HttpStatus.OK);
    }

    //通过ID查询用户
    @RequestMapping(value = "/queryUserById", method = RequestMethod.POST)
    public ResponseEntity queryTenantById(@RequestBody Map map) {
        HashMap res = new HashMap();
        Employees employee = userService.searchUserById(Long.parseLong((String) map.get("employeeId")));
        res.put("employee", employee);
        return new ResponseEntity(res, HttpStatus.OK);
    }

}
